<?php

namespace app\api\controller;

use app\common\controller\Base;
use app\common\model\Approves;
use app\common\model\ApprovesFlow;
use app\common\model\ApprovesLeave;
use app\common\model\LeaveType;
use app\common\model\Tasks;
use app\common\model\UserRule;
use app\common\model\UserRuleApprove;
use think\Db;

/**
 * 审批接口
 */
class Approve extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /*
     * 审批列表
     */
    public function index()
    {
        $type = $this->request->get('type','all');
        if (!$type){
            $this->error(__('Invalid parameters'));
        }
        $keywords = $this->request->get('keywords');
        //当类型为my时获取待我审批的列表
        if ($type == 'my'){
            $data = Approves::getMyWaitLists($keywords);
        }else{
            $data = Approves::getLists($type,$keywords);
        }
        $this->success('审批列表', $data);
    }

    /*
     * 审批详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        if (!$id){
            $this->error(__('Invalid parameters'));
        }
        $this->success('审批详情',Approves::getDetail($id));
    }


    /*
     * 审批流
     */
    public function flow()
    {
        $type = $this->request->get('type');
        if (!$type){
            $this->error(__('Invalid parameters'));
        }
        $data = ApprovesFlow::getTypeLists($type);
        $this->success('ok', $data);
    }


    /*
     * 请假类型
     */
    public function leave_type()
    {
        $this->success('请假类型',LeaveType::getLists());
    }

    /*
     * 请假审批
     */
    public function leave()
    {
        repeat_filter();        // 防抖

        $params = $this->request->post();
        //验证参数
        $this->apiValidate($params, \app\api\validate\Approve::class,'leave');

        $data = Approves::leaveEdit($params);
        $this->success('审批已提交',$data);
    }

    /*
     * 报销审批
     */
    public function reimburse()
    {
        repeat_filter();        // 防抖

        $params = $this->request->post();
        //验证参数
        $this->apiValidate($params, \app\api\validate\Approve::class,'reimburse');

        $data = Approves::reimburseEdit($params);
        $this->success('审批已提交',$data);
    }

    /*
     * 采购审批
     */
    public function purchase()
    {
        repeat_filter();    // 防抖
        $params = $this->request->post();
        //验证参数
        $this->apiValidate($params, \app\api\validate\Approve::class,'purchase');

        $data = Approves::purchaseEdit($params);
        $this->success('审批已提交',$data);
    }

    /*
     * 选择任务单 - 任务单列表
     */
    public function task()
    {
        $this->success('选择任务单',Tasks::getTasks());
    }

    /*
     * 审批通过
     */
    public function pass()
    {
        repeat_filter();        // 防抖

        $params = $this->request->post();
        //验证参数
        $this->apiValidate($params, \app\api\validate\Approve::class,'operStatus');

        $this->success('审批通过',Approves::operPass($params));
    }

    /*
     * 审批拒绝
     */
    public function refuse()
    {
        repeat_filter();        // 防抖

        $params = $this->request->post();
        //验证参数
        $this->apiValidate($params, \app\api\validate\Approve::class,'operStatus');

        $this->success('审批拒绝',Approves::operRefuse($params));
    }

    /*
     * 撤销审批
     */
    public function cancel()
    {
        $id = $this->request->post('id');
        if (!$id){
            $this->error(__('Invalid parameters'));
        }
        $this->success('取消成功',Approves::operCancel($id));
    }

}
